package com.qcby.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.admin.form.CategoryForm;
import com.qcby.admin.model.SgCategory;
import com.qcby.admin.dao.SgCategoryMapper;
import com.qcby.admin.model.SgTag;
import com.qcby.admin.service.SgCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.admin.vo.CategoryVO;
import com.qcby.admin.vo.PageVo;
import com.qcby.admin.vo.TagListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-08
 */
@Service
public class SgCategoryServiceImpl extends ServiceImpl<SgCategoryMapper, SgCategory> implements SgCategoryService {
    @Autowired
    private SgCategoryMapper sgCategoryMapper;
    @Override
    public PageVo selectCategoryPage(CategoryForm categoryForm) {
        //分页查询
        Page page=new Page(categoryForm.getPageNum(),categoryForm.getPageSize());
        QueryWrapper<SgCategory> queryWrapper = new QueryWrapper<>();
        //根据分类名进行查询
        queryWrapper.like(StringUtils.isNotBlank(categoryForm.getName()),"name",categoryForm.getName());
        //根据状态查询
        queryWrapper.eq(StringUtils.isNotBlank(categoryForm.getStatus()),"status",categoryForm.getStatus());
        //进行分页
        Page<SgCategory> result = sgCategoryMapper.selectPage(page, queryWrapper);
        List<CategoryVO> listVos = new ArrayList<>();
        for (SgCategory sgCategory : result.getRecords()) {
            CategoryVO categoryVO = new CategoryVO();
            categoryVO.setId(sgCategory.getId());
            categoryVO.setName(sgCategory.getName());
            categoryVO.setDescription(sgCategory.getDescription());
            categoryVO.setDelFlag(sgCategory.getDelFlag());
            categoryVO.setPid(sgCategory.getPid());
            categoryVO.setStatus(sgCategory.getStatus());
            listVos.add(categoryVO);
        }
        PageVo pageVo = new PageVo(listVos, listVos.size());
        //数据封装，返回
        return pageVo;
    }
}
